package com.quad.innovators.salesease.controller.warehouse;

import com.quad.innovators.salesease.annotation.DataPermission;
import com.quad.innovators.salesease.annotation.FunctionPermission;
import com.quad.innovators.salesease.annotation.Log;
import com.quad.innovators.salesease.annotation.RepeatSubmit;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.common.result.Result;
import com.quad.innovators.salesease.constants.SystemConstants;
import com.quad.innovators.salesease.enums.LogModuleEnum;
import com.quad.innovators.salesease.model.common.Option;
import com.quad.innovators.salesease.model.form.warehouse.SaleReturnOrderForm;
import com.quad.innovators.salesease.model.query.warehouse.SaleReturnOrderQuery;
import com.quad.innovators.salesease.model.vo.warehouse.SaleReturnOrderVO;
import com.quad.innovators.salesease.service.warehouse.SaleReturnOrderService;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2024/12/18 15:43
 * @Version : 1.0 
 */
@Tag(name = "12.销售退货单")
@RestController
@RequestMapping(SystemConstants.BACKEND_API + "/sale-return-order")
@RequiredArgsConstructor
public class SaleReturnOrderController {

    private final SaleReturnOrderService saleReturnOrderService;


    @Operation(summary = "分页列表")
    @GetMapping("/page")
    @Log(value = "分页列表", module = LogModuleEnum.SALE_RETURN_ORDER)
    @DataPermission(permissionCode = "warehouse:sale-return-order:data-permission")
    public Result<PageResultBean<SaleReturnOrderVO>> listPaged(SaleReturnOrderQuery queryParams) {
        PageResultBean<SaleReturnOrderVO> result = saleReturnOrderService.listPage(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "表单数据")
    @GetMapping("/{code}/form")
    @Parameters({
            @Parameter(name = "code", description = "code"),
    })
    @FunctionPermission(permissionCode = "warehouse:sale-return-order:detail")
    @DataPermission(permissionCode = "warehouse:sale-return-order:data-permission")
    public Result<SaleReturnOrderForm> getForm(@PathVariable("code") String code) {
        SaleReturnOrderForm formData = saleReturnOrderService.getFormData(code);
        return Result.success(formData);
    }

    @Operation(summary = "新增")
    @PostMapping
    @FunctionPermission(permissionCode = "warehouse:sale-return-order:add")
    @RepeatSubmit
    public Result<?> save(@RequestBody @Valid SaleReturnOrderForm form) {
        boolean result = saleReturnOrderService.save(form);
        return Result.success(result);
    }

    @Operation(summary = "修改")
    @PutMapping(value = "/{code}")
    @Parameters({
            @Parameter(name = "code", description = "code")
    })
    @FunctionPermission(permissionCode = "warehouse:sale-return-order:edit")
    @DataPermission(permissionCode = "warehouse:sale-return-order:data-permission")
    public Result<?> update(@PathVariable("code") String code, @RequestBody @Validated SaleReturnOrderForm form) {
        boolean result = saleReturnOrderService.update(code, form);
        return Result.success(result);
    }

    @Operation(summary = "订单状态变更")
    @PutMapping(value = "/{code}/updateStatus")
    @Parameters({
            @Parameter(name = "code", description = "code")
    })
    @FunctionPermission(permissionCode = "warehouse:sale-return-order:edit")
    public Result<?> update(@PathVariable("code") String code, Integer updateStatus) {
        boolean result = saleReturnOrderService.updateStatus(code, updateStatus);
        return Result.success(result);
    }

    @Operation(summary = "车辆带货绑定销售退货单")
    @GetMapping("/vehicle-bring/options")
    @DataPermission(permissionCode = "warehouse:sale-return-order:data-permission")
    public Result<List<Option<String>>> vehicleListOptions(@Parameter String vehicleBringGoodsCode) {
        List<Option<String>> result = saleReturnOrderService.vehicleListOptions(vehicleBringGoodsCode);
        return Result.success(result);
    }
}
